<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class School_model extends CI_Model {
	public function __construct() {
		parent::__construct ();
	}
	/*
	 * 返回学校的数量
	 */
	public function count_schools(){
		$query = $this->db->get('t_school');
		return $query->num_rows();
	}
	/*
	 * 返回学校信息
	 */
	public function get_schools($page, $offset){
		$this->db->limit($page, $offset);
		$query = $this->db->get('t_school');
		return $query->result_array();
	}
	/*
	 * 根据学校ID得到学校信息
	 */
	public function load($id){
		$query = $this->db->get_where('t_school',array('id' => $id));
		if ($row = $query->row_array()){
			return $row;
		}
		return array();
	}
	/*
	 * 添加学校信息
	 */
	public function create($school){
		$this->db->insert('t_school', $school);
		return $this->db->insert_id();
	}
	/*
	 * 更新学校信息
	 */
	public function update($id,$school){
		$this->db->where('id', $id);
		$this->db->update('t_school', $school);
		return $this->db->affected_rows()>0;
	}
	/*
	 * 删除学校信息
	 */
	public function delete($id){
		$this->db->where('id', $id);
		$this->db->delete('t_school');
		return $this->db->affected_rows()>0;
	}
	/*
	 *根据学校类型返回年级
	 */
	public function find_grade_by_type($type){
		$this->db->where('type', $type);
		$query = $this->db->get('t_grade');
		return $query->result_array();
	}
	/*
	 *根据学校ID返回班级数量
	 */
	public function count_class_by_school($id){
		$this->db->where('schoolid', $id);
		$query = $this->db->get('t_class');
		return $query->num_rows();
	}
	/*
	 *根据学校ID返回班级
	 */
	public function find_class_by_school($id,$page,$offset){
		$this->db->select('t_class.id,t_school.name as sname,t_grade.name as gname,t_class.name');
		$this->db->from('t_class');
		$this->db->where('schoolid', $id);
		$this->db->join('t_grade', 't_class.gradeid = t_grade.id','left');
		$this->db->join('t_school', 't_class.schoolid = t_school.id','left');
		$this->db->limit($page, $offset);
		$query = $this->db->get();
		return $query->result_array();
	}
	/*
	 * 添加班级信息
	 */
	public function createclass($class){
		$this->db->insert('t_class', $class);
		return $this->db->insert_id();
	}
	/*
	 * 删除班级信息
	 */
	public function deleteclass($id){
		$this->db->where('id', $id);
		$this->db->delete('t_class');
		return $this->db->affected_rows()>0;
	}
	/*
	 *返回所有班级
	 */
	public function find_all_class(){
		$this->db->select('t_class.id,t_school.name as sname,t_grade.name as gname,t_class.name');
		$this->db->from('t_class');
		$this->db->join('t_grade', 't_class.gradeid = t_grade.id','left');
		$this->db->join('t_school', 't_class.schoolid = t_school.id','left');
		$this->db->order_by("schoolid");
		$this->db->order_by("gradeid");			
		$query = $this->db->get();
		return $query->result_array();
	}
	/*
	 *返回所有学校
	 */
	public function find_all_school(){
		$query = $this->db->get('t_school');
		return $query->result_array();
	}
	/*
	 *根据学校ID返回班级
	 */
	public function find_class_by_schoolid($id){
		$this->db->select('t_class.id,t_school.name as sname,t_grade.name as gname,t_class.name');
		$this->db->from('t_class');
		$this->db->join('t_grade', 't_class.gradeid = t_grade.id','left');
		$this->db->join('t_school', 't_class.schoolid = t_school.id','left');
		$this->db->where('schoolid', $id);
		$query = $this->db->get();
		return $query->result_array();
	}
	/*
	 *根据班级ID返回班级信息
	 */
	public function find_class_by_id($id){
		$this->db->select('t_class.id,t_class.schoolid,t_class.gradeid,t_school.type');
		$this->db->from('t_class');
		$this->db->join('t_school', 't_class.schoolid = t_school.id','left');
		$this->db->where("t_class.id", $id);
		$query = $this->db->get();
		return  $query->row_array();
	}
	/*
	 *根据学校年级返回班级(用于导出学生订餐表)(新)
	 */
	public function find_class_by_school_grade($school,$grade){
		$this->db->select('t_class.id,t_school.name as sname,t_grade.name as gname,t_class.name');
		$this->db->from('t_class');
		$this->db->join('t_grade', 't_class.gradeid = t_grade.id','left');
		$this->db->join('t_school', 't_class.schoolid = t_school.id','left');
		$this->db->where('schoolid', $school);
		if($grade!=-1){
			$this->db->where('gradeid', $grade);
		}
		$query = $this->db->get();
		return $query->result_array();
	}
}
